#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
using namespace std;

int main()
{
	string type; cin >> type; cout << type << endl;
	bool DUN = 0, GN = 0; // 有向无向、带不带权
	if (type == "DG")  DUN = 1, GN = 0;
	if (type == "DN")  DUN = 1, GN = 1;
	if (type == "UDG") DUN = 0, GN = 0;
	if (type == "UDN") DUN = 0, GN = 1;
	int n; cin >> n; vector<string> nodes(n);
	map<string, int> ntoi; // node to index
	for (int i = 0; i < n; i++) {
		cin >> nodes[i]; if (i) cout << ' '; cout << nodes[i];
		ntoi[nodes[i]] = i;
	}	//cout << endl << endl;
	string flag("0"); cin >> flag;
	vector<vector<string>> graph(n, vector<string>(n, flag));
	int k; cin >> k; cout << endl << k << endl;
	vector<vector<string>> path(k, vector<string>(2 + GN)); // 带权的话 给权值留个位置
	for (int i = 0; i < k; i++) cin >> path[i][0] >> path[i][1];
	if (GN) for (int i = 0; i < k; i++)  cin >> path[i][2];
	for (auto& v : path) {
		graph[stoi(v[0])][stoi(v[1])] = GN ? v[2] : "1";
		if (!DUN) graph[stoi(v[1])][stoi(v[0])] = graph[stoi(v[0])][stoi(v[1])];
	}
	for (int i = 0; i < n; i++) {
		if (i)cout << endl;
		for (int j = 0; j < n; j++) {
			//if (j) cout << " ";
			cout << graph[i][j] << " ";
		}
	}

	cout << endl << endl;

	int u, v; cin >> u >> v;
	bool jug = false;
	int count = 0;
	if (u < 0 || v < 0 || u >= n || v >= n);
	else {
		count += graph[u][v] != flag;
		graph[u][v] = flag;
		if (graph[u][v] != flag) {
			count++; 
			graph[u][v] = flag; 
		}
		if (DUN == false && graph[v][u] != flag) {
			graph[v][u] = flag;
		}

		if (count) jug = true;
	}

	if (jug) cout << "true";
	else cout << "false";
	cout << endl << k - count << endl;
	for (int i = 0; i < n; i++) {
		if (i)cout << endl;
		for (int j = 0; j < n; j++) {
			//if (j) cout << " ";
			cout << graph[i][j] << " ";
		}
	}

	return 0;
}
// UDN 6 1 2 3 4 5 6 -1 6 0 1 0 2 1 3 2 3 3 4 3 5 20 30 40 50 70 80 0 3
// UDN 6 1 2 3 4 5 6 -1 6 0 1 0 2 1 3 2 3 3 4 3 5 20 30 40 50 70 80 0 2